package com.todod.service;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.mapper.ProjectFileRecordsMapper;
import com.todod.model.ProjectFile;
import com.todod.model.ProjectFileRecords;

@Service
@Transactional
public class ProjectFileRecordsService {

	@Autowired
	private ProjectFileRecordsMapper projectFileRecordsMapper;
	@Autowired
	private ProjectFileService projectFileService;

	/**
	 * @author dyl
	 * @date 2023-07-12
	 * @param _userId
	 * @param projectFileRecords
	 * @param value
	 * @param value2
	 */
	public void saveData(Long userId, Long fileId, String flag) {

		ProjectFileRecords projectFileRecords = new ProjectFileRecords();
		projectFileRecords.setId(SnowflakeIdGenerator.getId());
		projectFileRecords.setFileId(fileId);
		projectFileRecords.setUserId(userId);
		projectFileRecords.setFlag(flag);

		projectFileRecords.setCreatetimeTi(LocalDateTime.now());
		projectFileRecords.setCreateuserId(userId);
		projectFileRecordsMapper.insert(projectFileRecords);
	}

	/**
	 * @author dyl
	 * @date 2023-07-12
	 * @param _userId
	 * @param projectInfo
	 * @param value
	 * @param value2
	 */
	public void editData(Long _userId, Long fileId, String date, String downloads, String views) {

		ProjectFileRecords projectFileRecords1 = getEvaluateListByFileId(fileId, date);

		projectFileRecords1.setModifytimeTi(LocalDateTime.now());
		projectFileRecords1.setModifyuserId(_userId);
		projectFileRecordsMapper.updateById(projectFileRecords1);
	}

	/**
	 * @author dyl
	 * @date 2023-07-12
	 * @param code
	 * @return
	 */
	public List<ProjectFileRecords> getEvaluateListByFileId(Long fileId) {
		QueryWrapper<ProjectFileRecords> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfr_file_mid", fileId);

		List<ProjectFileRecords> list = projectFileRecordsMapper.selectList(queryWrapper);

		return list;
	}

	/**
	 * @author dyl
	 * @date 2023-07-12
	 * @param code
	 * @return
	 */
	public ProjectFileRecords getEvaluateListByFileId(Long fileId, String date) {
		QueryWrapper<ProjectFileRecords> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfr_file_mid", fileId);
		queryWrapper.eq("gfr_date", date);

		return projectFileRecordsMapper.selectOne(queryWrapper);
	}

	/**
	 * @author dyl
	 * @date 2023-07-12
	 * @param fileId
	 * @param date
	 * @return
	 */
	public boolean getEvaluateListByDate(Long fileId, String date) {
		QueryWrapper<ProjectFileRecords> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfr_file_mid", fileId);
		queryWrapper.eq("gfr_date", date);

		List<ProjectFileRecords> list = projectFileRecordsMapper.selectList(queryWrapper);
		if (list.size() > 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * @author dyl
	 * @date 2023-07-12
	 * @param format
	 * @return
	 */
	public List<ProjectFileRecords> getSortByDate(String format) {
		QueryWrapper<ProjectFileRecords> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfr_date", format);

		List<ProjectFileRecords> list = projectFileRecordsMapper.selectList(queryWrapper);

		return list;
	}

	public boolean getTimeComparison(Long _userId, Long xftId, String originalName) {
		ProjectFileRecords projectFileRecords = projectFileRecordsMapper.getTimeComparison(_userId, xftId,
				originalName);

		if (projectFileRecords != null) {
			// 判断下载时间和上传时间大小
			int result = projectFileRecords.getDownTime().compareTo(projectFileRecords.getUploadTime());
			if (result >= 0) {// 如果下载时间晚于或者等于上传时间
				if (projectFileRecords.getDownUser().equalsIgnoreCase(projectFileRecords.getUploadUser())) {// 如果下载人员等于上传人员
					return true;
				} else {
					return false;
				}
			} else {
				if (projectFileRecords.getDownUser().equalsIgnoreCase(projectFileRecords.getUploadUser())) {// 如果下载人员等于上传人员
					return true;
				} else {
					return false;
				}
			}
		} else {
			ProjectFile projectFile = projectFileService.selectByXftId(xftId, originalName);
			if (projectFile != null) {
				if (_userId.equals(projectFile.getCreateuserId())) {// 如果本次上传人员等于上次上传人员
					return true;
				} else {
					return false;
				}
			} else {
				return true;
			}
		}
	}

}
